from dao import Dao
from model.house import House
from bson.dbref import DBRef

class HouseDao(Dao):
    def __init__(self):
        Dao.__init__(self, House)
        self.collection = 'houses'
        
    def insert(self, item):
        """Save the object to collection
        Arguments:
            item --- The Address Model object
        """
        if isinstance(item, House):
            # Insert except ID field, because ID will be automatically generated
            _dict = {}
            # Base class fields
            _dict['name'] = item.name
            _dict['description'] = item.description
            _dict['year'] = item.year
            _dict['price'] = item.price
            _dict['status'] = item.status
            
            # Sub class fields
            _dict['style'] = item.style
            _dict['n_rooms'] = item.n_rooms
            _dict['n_baths'] = item.n_baths
            _dict['n_garages'] = item.n_garages
            _dict['basement'] = item.basement
            _dict['lots'] = item.lots
            
            _dict['living_space'] = item.living_space
            _dict['faces'] = item.faces
            _dict['deck'] = item.deck
            _dict['patio'] = item.patio
            _dict['fence'] = item.fence
            _dict['ac'] = item.ac
            
            # Associate field
            _dict['address'] = DBRef('addresses', item.address._id );
            
            return self.db.insert(self.collection, _dict)

    def save(self, model):
        """Updates an existing document or inserts a new document, depending on its document parameter.
        Arguments:
            model --- The Model object with id if model is a dict type, the dbref field must be DBRef type
        Return:
            The '_id' value of to_save or [None] if manipulate is False and to_save has no _id field.
        """
        # The ID field must exist
        if isinstance(model, dict):
            return self.db.save(self.collection, model)
        else:            
            _dict = model.__dict__
            if _dict is not None:
                _dict['address'] = DBRef('addresses', model.address._id)
                return self.db.save(self.collection, _dict)
            else:
                return None
        
    def find_one(self, query):
        """
        Arguments:
            query --- The dict type, the ref field must have DBRef type
        Return:
            Raises TypeError if any of the arguments are of improper type. 
            Returns an instance of Cursor corresponding to this query.
        """
        _dict = self.db.find_one(self.collection, query)
        if _dict is not None:
            _dbref = _dict['address']
            _dict['address'] = self.db.find_one('addresses', {'_id':_dbref.id})
            return self.ModelClass(_dict)
        else:
            return None
    
    def find(self, query={}):
        """
        Arguments:
            query --- The dict type
        Return:
            Raises TypeError if any of the arguments are of improper type. 
            Returns an instance of Cursor corresponding to this query.
        """
        cursor = self.db.find(self.collection, query=query)
        rets = []
        for _dict in cursor:
            _dict['address'] = self.db.find_one('addresses', {'_id':_dict['address']._id})
            rets.append(_dict)
        return rets            